﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.BusinessLogic.Clusters
{
    public class DictionaryUtil:IDisposable
    {
        private List<string> words=new List<string>();
        public List<string> Words
        {
            get
            {
                return words;
            }
        }

        public DictionaryUtil(string connStr, string tblName, string termField)
        {
            SqlConnection conn=new SqlConnection(connStr);
            try
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = string.Format("select {0} from {1}", termField, tblName);
                SqlDataReader reader = cmd.ExecuteReader();
                this.words = new List<string>();
                while (reader.Read())
                {
                    this.words.Add(reader.GetString(0));
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandlesException(ex);
            }
            finally
            {
                conn.Close();
            }
        }

        #region Implementation of IDisposable
        public void Dispose()
        {
            this.words = null;
        }

        #endregion
    }
}
